import type { NextApiRequest, NextApiResponse } from 'next'
import Cors from 'cors';
import initMiddleware from '@/utils/init-middleware';
import { setCookie } from '@/utils/cookies';

// Initialize the cors middleware
const cors = initMiddleware(
  // You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
  Cors({
    // Only allow requests with GET, POST and OPTIONS
    methods: ['GET', 'POST', 'OPTIONS'],
  })
)

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  // Run cors
  await cors(req, res)

  setCookie(res, 'Next.js', 'api-middleware!')
  res.json({'Set-Cookie': res.getHeader('Set-Cookie')})
}